<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>表单验证</title>
  <style>
    /* css reset (样式重置)*/
    * {
      margin: 0;
      padding: 0;
    }

    ul,
    ol {
      list-style: none;
    }

    a {
      text-decoration: none;
    }

    #container {
      border: 1px solid #000;
      width: 300px;
      margin: 100px auto;
    }

    #container h3 {
      text-align: center;
    }

    .form-group{
      display:flex;
      margin-bottom: 20px;
    }

    .form-label{
      flex: 4;
      text-align: right;
      margin-right: 10px;
    }

    .form-input{
      flex: 8;
    }

    .form-input .error{
      display: none;
      color: #ff0000;
    }

  </style>
</head>

<body>
  <div id="container">
    <h3>用户注册</h3>
    <div class="form-group">
      <label for="username" class="form-label">用户名:</label>
      <div class="form-input">
        <input type="text" id="username">
        <span class="error"></span>
      </div>
    </div>
    <div class="form-group">
      <label for="password" class="form-label">密码:</label>
      <div class="form-input">
        <input type="password" id="password">
        <span class="error"></span>
      </div>
    </div>
    <div class="form-group">
      <label for="re-password" class="form-label">确认密码:</label>
      <div class="form-input">
        <input type="password" id="re-password">
        <span class="error"></span>
      </div>
    </div>
    <div class="form-group">
      <label class="form-label">性别:</label>
      <div class="form-input">
        <input type="radio" name="gender">男
        <input type="radio" name="gender">女
        <span class="error"></span>
      </div>
    </div>
    <div class="form-group">
      <label for="geade" class="form-label">年级:</label>
      <div class="form-input">
        <select id="grade">
          <option value="-1">请选择</option>
          <option value="2021">2021级</option>
          <option value="2020">2020级</option>
          <option value="2019">2019级</option>
        </select>
        <span class="error"></span>
      </div>
    </div>
    <div class="form-group">
      <label class="form-label">爱好:</label>
      <div class="form-input">
        <input type="checkbox" name="hobby">唱歌
        <input type="checkbox" name="hobby">跳舞
        <input type="checkbox" name="hobby">画画
        <span class="error"></span>
      </div>
    </div>
    <div class="form-group">
      <label for="email" class="form-label">邮箱:</label>
      <div class="form-input">
        <input type="text" id="email">
        <span class="error"></span>
      </div>
    </div>
    <div class="form-group">
      <label class="form-label"></label>
      <div class="form-input">
        <button id="btn-submit">提交</button>
      </div>
    </div>

  <script>
    // 获取提交的按钮
    let btnSubmit = document.getElementById("btn-submit");
    let username = document.getElementById("username");
    let password = document.getElementById("password");
    let rePassword = document.getElementById("re-password");
    let grade = document.getElementById("grade");
    let email = document.getElementById("email");

    let genders = document.getElementsByName("gender"); // genders是一个数组,数组里有两个DOM对象
    let hobbies = document.getElementsByName("hobby");

    // let usernameError = document.getElementById("username-error");
    // let passwordError = document.getElementById("password-error");

    
    // 为这个按钮对象添加一个点击的事件（方法）
    btnSubmit.onclick = function () { //onclick点击事件（方法）
      // 用户名不能为空
      if (username.value.trim() == "") {// value属性用来获取输入框的值 trim()用来去掉首尾空格
        // alert('请输入用户名');
        showError(username,'请输入用户名');

        // username.focus(); // 让用户名输入框获得焦点
      }else{
        hideError(username);
      }
      // 密码长度不能少于6位
      if (password.value.trim().length < 6) {
        // alert('密码长度不能小于6');
        showError(password,'密码长度不能小于6');
        // password.focus();
      }else{
        hideError(password);
      }
      // // 确认密码和密码一致
      if (rePassword.value == ''){
        showError(rePassword,'请重新输入密码');
      }else if(rePassword.value.trim() != password.value){
        showError(rePassword,'两次密码不一致');
      }else{
        hideError(rePassword);
      }
      //   // alert('两次密码不一致')
      // rePassword.focus();

      // 性别必须至少选择一个
      // 只需要判断两个radio输入框中有一个checked属性有值就可以
      // 当业务需要在两种状态来回切换是，弄一个布尔型的标志位

      /* for (let i=0; i<genders.length; i++){ //这段代码逻辑有问题
        let gender = genders[i];
        if(gender.checked){
          break;
        }else{
          alert('请至少选择一个性别');
        }
      }  */
      check(genders,'请至少选择一个性别');

      // // 判断年级
      if(grade.value == '-1'){
        showError(grade,'请选择一个年级');
      //   // alert('请选择一个年级');
      //   grade.focus();
      }else{
        hideError(grade);
      }

      // 判断爱好
      check(hobbies,'请至少选择一个爱好');

      // 判断email
      if(email.value == ''){
        showError(email,'请输入邮箱');
        // alert('邮箱必须包含@');
        // email.focus();
      // }
      }else if(email.value.trim().indexOf('@') == -1){
        showError(email,'邮箱必须包含@');
      }else{
        hideError(email);
    }
  } 
      function check(elems,message){
        let isChecked = false; // 这是一个标识位，false代表没选中，true代表选中
      for (let i = 0; i < elems.length; i++) {
        let elem = elems[i];
        if (elem.checked) {
          isChecked = true; //只要有一个gender被选中，genderIsChecked就被改为true，代表已经被选中
          break;
        }
      }
      if (!isChecked) {
        showError(elems[elems.length -1], message);
      }else{
        hideError(elems[elems.length -1]);
      }

    }
    function showError(elem,message){
      let error = elem.nextElementSibling;//将username.nextElementSibling缓存给usernameError变量
        error.innerHTML = message;
        error.style.display = 'block';
    }

    function hideError(elem){
      let error = elem.nextElementSibling;
        error.innerHTML = "";
        error.style.display = 'none';
    }

  </script>
</body>

</html>